﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Utility;
using Model;
using System.Collections.Generic;
using BLL;

public partial class VehicleUI_Fuel_CashFuelAMV : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindEmployee();
            if (Request.QueryString["Type"] == null)
            {
                if (Session["FilterFrameNO"] != null)
                {
                    txt_FrameNO.Text = Session["FilterFrameNO"].ToString();
                }
            }
            else if (Request.QueryString["Type"].ToString() == "View")
            {
                LoadService();
                txt_VehicleID.Enabled = false;
                txt_FuelTime.Enabled = false;
                txt_FuelMoney.Enabled = false;
                txt_Address.Enabled = false;
                txt_FrameNO.Enabled = false;
                ddl_Employee.Enabled = false;
                txt_Reason.Enabled = false;

                btn_Sure.Visible = false;
            }
            else if (Request.QueryString["Type"].ToString() == "Edit")
            {
                LoadService();
            }
        }
    }

    private void BindEmployee()
    {
        IList<Employee> list = new EmployeeBLL().GetAllEmployee((int)Session["OrganID"]);
        ddl_Employee.DataSource = list;
        ddl_Employee.DataTextField = "Name";
        ddl_Employee.DataValueField = "EmployeeID";
        ddl_Employee.DataBind();
    }

    protected void LoadService()
    {
        int _id = Convert.ToInt32(Request.QueryString["ID"]);
        CashFuel cs = new CashFuelBLL().GetModel(_id);
        if (cs != null)
        {
            txt_VehicleID.Text = cs.VehicleID;
            txt_FrameNO.Text = cs.FrameNO;
            txt_FuelTime.Text = cs.FuelTime.ToString();
            txt_FuelMoney.Text = cs.FuelMoney.ToString();
            txt_Address.Text = cs.Address == null ? "" : cs.Address;
            txt_Reason.Text = cs.Reason == null ? "" : cs.Reason;
            ddl_Employee.SelectedValue = cs.Operator.ToString();
        }
    }

    protected void btn_Sure_Click(object sender, EventArgs e)
    {
        string _frameNO = txt_FrameNO.Text.Trim();
        bool reExist = new VehicleBLL().Exists(_frameNO);
        if (!reExist)
        {
            UtilityService.Alert(this.Page, "不存在该车辆信息,请重新录入车架号!");
            txt_FrameNO.Focus();
            return;
        }

        if (!ValidateService.IsNumber(txt_FuelMoney.Text.Trim()))
        {
            UtilityService.Alert(this.Page, "金额格式错误!");
            txt_FuelMoney.Focus();
            return;
        }

        if (Request.QueryString["Type"] == null)
        {
            CashFuel cs = new CashFuel();

            cs.FrameNO = txt_FrameNO.Text.Trim();
            cs.FuelTime = DateTime.Parse(txt_FuelTime.Text.Trim());
            cs.Operator = ddl_Employee.SelectedValue.ToString();
            cs.Address = txt_Address.Text.Trim();
            cs.FuelMoney = decimal.Parse(txt_FuelMoney.Text.Trim());
            cs.Reason = txt_Reason.Text.Trim();
            cs.OrganID = (int)Session["OrganID"];
            cs.InputBy = Session["UserID"].ToString();

            int re = new CashFuelBLL().Add(cs);

            if (re > 0)
            {
                string msg = "";
                UtilityService.AlertAndRedirect(this.Page, "添加成功! " + msg, "CashFuelMgr.aspx");
            }
            else
            {

                UtilityService.Alert(this.Page, "添加失败! ");
            }
        }
        else if (Request.QueryString["Type"] == "Edit")
        {
            int _id = Convert.ToInt32(Request.QueryString["ID"]);
            CashFuel cs = new CashFuelBLL().GetModel(_id);
            cs.ID = _id;
            cs.FrameNO = txt_FrameNO.Text.Trim();
            cs.FuelTime = DateTime.Parse(txt_FuelTime.Text.Trim());
            cs.Operator = ddl_Employee.SelectedValue.ToString();
            cs.Address = txt_Address.Text.Trim();
            cs.FuelMoney = decimal.Parse(txt_FuelMoney.Text.Trim());
            cs.Reason = txt_Reason.Text.Trim();

            bool re = new CashFuelBLL().Update(cs);

            if (re)
            {
                string msg = "";
                UtilityService.AlertAndRedirect(this.Page, "修改成功! " + msg, "CashFuelMgr.aspx");
            }
            else
            {
                UtilityService.Alert(this.Page, "修改失败! ");
            }
        }
    }
    protected void btn_Back_Click(object sender, EventArgs e)
    {
        Response.Redirect("CashFuelMgr.aspx");
    }
}
